package com.youth.config.redis;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName RedissonDistributedLocker
 * @Description: TODO
 * @Author yjwu
 * @Date 2019/10/30 22:38
 **/
@Component
public class RedissonDistributedLocker implements IDistributedLocker {
    @Autowired
    private RedissonClient redissonClient;
    @Value("${redissonLockerTimeout}")
    private int redissonLockerTimeout;

    @Override
    public void lock(String key) {
        RLock rLock = redissonClient.getLock(key);
        rLock.lock(redissonLockerTimeout, TimeUnit.MILLISECONDS);
    }

    @Override
    public void lock(String key, int timeout, TimeUnit timeUnit) {
        RLock rLock = redissonClient.getLock(key);
        rLock.lock(timeout, timeUnit);
    }

    @Override
    public void unLock(String key) {
        RLock rLock = redissonClient.getLock(key);
        rLock.lock();

    }
}
